using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VKSART
    {
        public DataRow Convert2KSART(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryKSART(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KSAMANDANT"] = " ".PadRight(2 ); 
         dwReturn["KSAKSTKRS"] = " ".PadRight(2 ); 
         dwReturn["KSAKST"] = " ".PadRight(4 ); 
         dwReturn["KSAART"] = " ".PadRight(6 ); 
         dwReturn["KSAAENZL"] = " ".PadRight(0 ); 
         dwReturn["KSAAENB"] = " ".PadRight(8 ); 
         dwReturn["KSAAEND"] = " ".PadRight(0 ); 
         dwReturn["KSANEUB"] = " ".PadRight(8 ); 
         dwReturn["KSANEUD"] = " ".PadRight(0 ); 
         dwReturn["KSASTA"] = " ".PadRight(1 ); 
         dwReturn["KSADATAB"] = " ".PadRight(0 ); 
         dwReturn["KSADATBIS"] = " ".PadRight(0 ); 
         dwReturn["KSABUDGET"] = " ".PadRight(1 ); 
         dwReturn["KSASTEUER"] = " ".PadRight(1 ); 
         dwReturn["KSAFIBUKTO"] = " ".PadRight(15 ); 
         dwReturn["KSAARTGRP"] = " ".PadRight(4 ); 
         dwReturn["KSAFILLER"] = " ".PadRight(30 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapKSART(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveKSART(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryKSART(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapKSART(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertKSART(dwLocal, ITXDBVERSION);
            }
            UpdateKSART(dwLocal);
        }
        public void InsertKSART(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO KSART(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryKSART(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM KSART WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM KSART  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "KSART";
            Return = Result;
            return Return;
        }
        public void DeleteKSART(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM KSART   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateKSART(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE KSART SET "
                    +"KSAMANDANT= :KSAMANDANT,"
                    +"KSAKSTKRS= :KSAKSTKRS,"
                    +"KSAKST= :KSAKST,"
                    +"KSAART= :KSAART,"
                    +"KSAAENZL= :KSAAENZL,"
                    +"KSAAENB= :KSAAENB,"
                    +"KSAAEND= :KSAAEND,"
                    +"KSANEUB= :KSANEUB,"
                    +"KSANEUD= :KSANEUD,"
                    +"KSASTA= :KSASTA,"
                    +"KSADATAB= :KSADATAB,"
                    +"KSADATBIS= :KSADATBIS,"
                    +"KSABUDGET= :KSABUDGET,"
                    +"KSASTEUER= :KSASTEUER,"
                    +"KSAFIBUKTO= :KSAFIBUKTO,"
                    +"KSAARTGRP= :KSAARTGRP,"
                    +"KSAFILLER= :KSAFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KSAMANDANT", OracleType.NVarChar).Value = dwData["KSAMANDANT"].ToString(); 
          or1.Parameters.Add("KSAKSTKRS", OracleType.NVarChar).Value = dwData["KSAKSTKRS"].ToString(); 
          or1.Parameters.Add("KSAKST", OracleType.NVarChar).Value = dwData["KSAKST"].ToString(); 
          or1.Parameters.Add("KSAART", OracleType.NVarChar).Value = dwData["KSAART"].ToString(); 
          or1.Parameters.Add("KSAAENZL", OracleType.NVarChar).Value = dwData["KSAAENZL"].ToString(); 
          or1.Parameters.Add("KSAAENB", OracleType.NVarChar).Value = dwData["KSAAENB"].ToString(); 
          or1.Parameters.Add("KSAAEND", OracleType.NVarChar).Value = dwData["KSAAEND"].ToString(); 
          or1.Parameters.Add("KSANEUB", OracleType.NVarChar).Value = dwData["KSANEUB"].ToString(); 
          or1.Parameters.Add("KSANEUD", OracleType.NVarChar).Value = dwData["KSANEUD"].ToString(); 
          or1.Parameters.Add("KSASTA", OracleType.NVarChar).Value = dwData["KSASTA"].ToString(); 
          or1.Parameters.Add("KSADATAB", OracleType.NVarChar).Value = dwData["KSADATAB"].ToString(); 
          or1.Parameters.Add("KSADATBIS", OracleType.NVarChar).Value = dwData["KSADATBIS"].ToString(); 
          or1.Parameters.Add("KSABUDGET", OracleType.NVarChar).Value = dwData["KSABUDGET"].ToString(); 
          or1.Parameters.Add("KSASTEUER", OracleType.NVarChar).Value = dwData["KSASTEUER"].ToString(); 
          or1.Parameters.Add("KSAFIBUKTO", OracleType.NVarChar).Value = dwData["KSAFIBUKTO"].ToString(); 
          or1.Parameters.Add("KSAARTGRP", OracleType.NVarChar).Value = dwData["KSAARTGRP"].ToString(); 
          or1.Parameters.Add("KSAFILLER", OracleType.NVarChar).Value = dwData["KSAFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
